// pages/home/shop/shop.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopid:"",
    show: false,
    toter: 0,
    shoping: [],
    good: "",
    goods: [{
        goodsid: 1,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 2,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 3,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 4,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 5,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 6,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16"
      },
      {
        goodsid: 7,
        url: "https://img.yzcdn.cn/vant/cat.jpeg",
        name: "蛋炒饭",
        material: "蛋，饭",
        price: "16",

      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
console.log(wx.getStorageSync(options.shopid))
this.setData({
shopid:options.shopid,
// shoping:wx.getStorageSync(options.shopid),
})
if (wx.getStorageSync(options.shopid)!="") {
  console.log(456)
let shoping = wx.getStorageSync(options.shopid)
// console.log(shoping)
let toter  = 0
 for (let a = 0; a < shoping.length; a++) {
   
  //  console.log(a)
      let newtotal = Number(shoping[a].total)+toter;
      toter = newtotal
      // console.log(toter)
      // console.log(newtotal)
}
  
  this.setData({
    shopid:options.shopid,
    shoping:wx.getStorageSync(options.shopid),
    toter: Number(toter*100)
    })
}

// else(
//   // console.log(78798),
//   this.setData({
//     shopid:options.shopid,
//     shoping:wx.getStorageSync(options.shopid),
//     })
// )

wx.request({
  url: '',
  method:"POST",
  header:{
    "cookie":wx.getStorageSync('sessionid'),
    "token":wx.getStorageSync('session'),
  }, 
 data:{
   shopid:options.shopid,
 },
 success (res) {
  console.log(res)
  that.data.goods=res.data.goods
}
})
 
  },
  addto(e) {
    let i = e.currentTarget.dataset.index
    let that = this
    let good = that.data.goods
    let shoping = that.data.shoping
    this.setData({
      good: good[i]
    })
    this.setData({
      "good.number": 1,
      "good.total": that.data.goods[i].price
    })
    // console.log(this.data.good)
    // console.log(good[i].price)
    if (shoping == "") {
      let toter = parseInt((that.data.toter + good[i].price) * 100)
      // console.log(good[i])
      this.setData({
        shoping: that.data.shoping.concat(that.data.good),
        toter: toter,
      })
      // console.log(that.data.shoping)
    } else {
      for (let j = 0; j <= shoping.length; j++) {
        if (shoping[j].goodsid == good[i].goodsid) {
          let toter = parseInt(that.data.toter + good[i].price * 100)
          // console.log(toter)
          let number = "shoping[" + j + "].number"
          let total = "shoping[" + j + "].total"
          let newnumber = parseInt(shoping[j].number + 1)
          let price = shoping[j].price
          let totals = shoping[j].total
          let newtotal = Number(totals) + Number(price) 
          console.log(newtotal)
          this.setData({
            [number]: newnumber,
            [total]:newtotal,
            toter: toter,
          })
          // console.log(that.data.shoping[j])
        } else {
          let toter = parseInt(that.data.toter + good[i].price * 100)
          this.setData({
            shoping: that.data.shoping.concat(that.data.good),
            toter: toter,
          })
          // console.log(that.data.shoping)
 
        }

      }
    }
  },
  showPopup() {
    // console.log(666)
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      shoptotal:that.data.toter,
      shoping: that.data.shoping//参数
    })
    wx.setStorage({
      data: that.data.shoping,
      key: that.data.shopid,
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */

  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})